package com.jwdf.uda.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import com.jwdf.uda.R;
import com.makeramen.roundedimageview.RoundedImageView;

public class FittedImageView extends RoundedImageView {

    public static final int MODE_WIDTH = 0x1;
    public static final int MODE_HEIGHT = 0x2;
    public static final int MODE_ADAPTIVE= 0x3;
    private float mRatio = 1;
    private int mMode = MODE_WIDTH;

    public FittedImageView(Context context) {
        super(context);
    }

    public FittedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.FittedImageView);
        try {
            mRatio = a.getFloat(R.styleable.FittedImageView_image_ratio, 1);
            mMode = a.getInteger(R.styleable.FittedImageView_image_mode, MODE_WIDTH);
        } finally {
            a.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(mMode == MODE_ADAPTIVE) {
            try {
                Drawable drawable = getDrawable();
                if (drawable == null) {
                    setMeasuredDimension(0, 0);
                } else {
                    int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
                    int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
                    if (measuredHeight == 0 && measuredWidth == 0) { //Height and width set to wrap_content
                        setMeasuredDimension(measuredWidth, measuredHeight);
                    } else if (measuredHeight == 0) { //Height set to wrap_content
                        int width = measuredWidth;
                        int height = width *  drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
                        setMeasuredDimension(width, height);
                    } else if (measuredWidth == 0){ //Width set to wrap_content
                        int height = measuredHeight;
                        int width = height * drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight();
                        setMeasuredDimension(width, height);
                    } else { //Width and height are explicitly set (either to match_parent or to exact value)
                        setMeasuredDimension(measuredWidth, measuredHeight);
                    }
                }
            } catch (Exception e) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }else{
            setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
            int childWidthSize;
            int childHeightSize;
            if (mMode == MODE_WIDTH) {
                childWidthSize = getMeasuredWidth();
                childHeightSize = (int) (childWidthSize * mRatio);
            } else  if (mMode == MODE_HEIGHT){
                childHeightSize = getMeasuredHeight();
                childWidthSize = (int) (childHeightSize * mRatio);
            }else{
                childWidthSize = getMeasuredWidth();
                childHeightSize = getMeasuredHeight();
            }
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
